package org.lonetree.tc.webapp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.User;
import org.lonetree.tc.core.User.Username;
import org.lonetree.tc.core.dao.DAOFactory;
import org.lonetree.tc.core.dao.UserDAO;
import org.lonetree.tc.webapp.forms.UserForm;

public class CreateUser extends SecureAction {

	@Override
	public ActionForward executeSecure(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		try{
			UserForm nuf = (UserForm)form;
			Username uid = new Username(nuf.getUsername());
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
			UserDAO udao = factory.getUserDAO();
			request.setAttribute("userid", uid);
			udao.createUser(uid);
			return mapping.findForward("success");
		}
		catch(Exception e){
			return mapping.findForward("failure");
		}
		
	}

}
